@@ -5,7 +5,7 @@ env: |
||
5 | 5 |
- APP_SECRET_TOKEN=b2724973fd81c2f4ac0f92ac48eb3f0152c4a11824c122bcf783419a4c51d8b9bba81c8ba6a66c7de599677c7f486242cf819775c433908e77c739c5c8ae118d |
6 | 6 |
matrix: |
7 | 7 |
- DATABASE_ADAPTER=mysql2 |
8 |
- - DATABASE_ADAPTER=postgresql DATABASE_USERNAME=postgres ON_HEROKU=true |
|
8 |
+ - DATABASE_ADAPTER=postgresql DATABASE_USERNAME=postgres |
|
9 | 9 |
rvm: |
10 | 10 |
- 2.0.0 |
11 | 11 |
- 2.1.5 |
@@ -3,6 +3,15 @@ source 'https://rubygems.org' |
||
3 | 3 |
# Ruby 2.0 is the minimum requirement |
4 | 4 |
ruby ['2.0.0', RUBY_VERSION].max |
5 | 5 |
|
6 |
+# Load vendored dotenv gem and .env file |
|
7 |
+require File.join(File.dirname(__FILE__), 'lib/gemfile_helper.rb') |
|
8 |
+GemfileHelper.load_dotenv do |dotenv_dir| |
|
9 |
+ path dotenv_dir do |
|
10 |
+ gem 'dotenv' |
|
11 |
+ gem 'dotenv-rails' |
|
12 |
+ end |
|
13 |
+end |
|
14 |
+ |
|
6 | 15 |
# Optional libraries. To conserve RAM, comment out any that you don't need, |
7 | 16 |
# then run `bundle` and commit the updated Gemfile and Gemfile.lock. |
8 | 17 |
gem 'twilio-ruby', '~> 3.11.5' # TwilioAgent |
@@ -64,7 +73,6 @@ gem 'daemons', '~> 1.1.9' |
||
64 | 73 |
gem 'delayed_job', '~> 4.0.0' |
65 | 74 |
gem 'delayed_job_active_record', :git => 'https://github.com/cantino/delayed_job_active_record', :branch => 'configurable-reserve-sql-strategy' |
66 | 75 |
gem 'devise', '~> 3.4.0' |
67 |
-gem 'dotenv-rails', '~> 2.0.1' |
|
68 | 76 |
gem 'em-http-request', '~> 1.1.2' |
69 | 77 |
gem 'faraday', '~> 0.9.0' |
70 | 78 |
gem 'faraday_middleware', github: 'lostisland/faraday_middleware', branch: 'master' # '>= 0.10.1' |
@@ -83,7 +91,6 @@ gem 'kaminari', '~> 0.16.1' |
||
83 | 91 |
gem 'kramdown', '~> 1.3.3' |
84 | 92 |
gem 'liquid', '~> 3.0.3' |
85 | 93 |
gem 'mini_magick' |
86 |
-gem 'mysql2', '~> 0.3.16' |
|
87 | 94 |
gem 'multi_xml' |
88 | 95 |
gem 'nokogiri', '1.6.6.4' |
89 | 96 |
gem 'omniauth' |
@@ -136,22 +143,33 @@ gem 'tzinfo', '>= 1.2.0' # required by rails; 1.2.0 has support for *BSD and Sol |
||
136 | 143 |
# Windows does not have zoneinfo files, so bundle the tzinfo-data gem. |
137 | 144 |
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] |
138 | 145 |
|
139 |
-# Introduces a scope for Heroku specific gems. |
|
140 |
-def on_heroku |
|
141 |
- if ENV['ON_HEROKU'] || |
|
142 |
- ENV['HEROKU_POSTGRESQL_ROSE_URL'] || |
|
143 |
- ENV['HEROKU_POSTGRESQL_GOLD_URL'] || |
|
144 |
- File.read(File.join(File.dirname(__FILE__), 'Procfile')) =~ /intended for Heroku/ |
|
146 |
+ |
|
147 |
+# Introduces a scope for gem loading based on a condition |
|
148 |
+def if_true(condition) |
|
149 |
+ if condition |
|
145 | 150 |
yield |
146 | 151 |
else |
147 |
- # When not on Heroku, we still want our Gemfile.lock to include |
|
148 |
- # Heroku specific gems, so we scope them to an unsupported |
|
149 |
- # platform. |
|
152 |
+ # When not including the gems, we still want our Gemfile.lock |
|
153 |
+ # to include them, so we scope them to an unsupported platform. |
|
150 | 154 |
platform :ruby_18, &proc |
151 | 155 |
end |
152 | 156 |
end |
153 | 157 |
|
154 |
-on_heroku do |
|
155 |
- gem 'pg' |
|
158 |
+on_heroku = ENV['ON_HEROKU'] || |
|
159 |
+ ENV['HEROKU_POSTGRESQL_ROSE_URL'] || |
|
160 |
+ ENV['HEROKU_POSTGRESQL_GOLD_URL'] || |
|
161 |
+ File.read(File.join(File.dirname(__FILE__), 'Procfile')) =~ /intended for Heroku/ |
|
162 |
+ |
|
163 |
+ENV['DATABASE_ADAPTER'] ||= 'postgresql' if on_heroku |
|
164 |
+ |
|
165 |
+if_true(on_heroku) do |
|
156 | 166 |
gem 'rails_12factor', group: :production |
157 | 167 |
end |
168 |
+ |
|
169 |
+if_true(ENV['DATABASE_ADAPTER'].strip == 'postgresql') do |
|
170 |
+ gem 'pg', '~> 0.18.3' |
|
171 |
+end |
|
172 |
+ |
|
173 |
+if_true(ENV['DATABASE_ADAPTER'].strip == 'mysql2') do |
|
174 |
+ gem 'mysql2', '~> 0.3.16' |
|
175 |
+end |
@@ -58,6 +58,13 @@ GIT |
||
58 | 58 |
activerecord (>= 3.0, < 5.0) |
59 | 59 |
delayed_job (>= 3.0, < 4.1) |
60 | 60 |
|
61 |
+PATH |
|
62 |
+ remote: vendor/gems/dotenv-2.0.1 |
|
63 |
+ specs: |
|
64 |
+ dotenv (2.0.1) |
|
65 |
+ dotenv-rails (2.0.1) |
|
66 |
+ dotenv (= 2.0.1) |
|
67 |
+ |
|
61 | 68 |
GEM |
62 | 69 |
remote: https://rubygems.org/ |
63 | 70 |
specs: |
@@ -162,9 +169,6 @@ GEM |
||
162 | 169 |
docile (1.1.5) |
163 | 170 |
domain_name (0.5.24) |
164 | 171 |
unf (>= 0.0.5, < 1.0.0) |
165 |
- dotenv (2.0.1) |
|
166 |
- dotenv-rails (2.0.1) |
|
167 |
- dotenv (= 2.0.1) |
|
168 | 172 |
dropbox-api (0.4.2) |
169 | 173 |
hashie |
170 | 174 |
multi_json |
@@ -346,7 +350,7 @@ GEM |
||
346 | 350 |
multi_json (~> 1.3) |
347 | 351 |
omniauth-oauth (~> 1.0) |
348 | 352 |
orm_adapter (0.5.0) |
349 |
- pg (0.17.1) |
|
353 |
+ pg (0.18.3) |
|
350 | 354 |
polyglot (0.3.5) |
351 | 355 |
protected_attributes (1.0.8) |
352 | 356 |
activemodel (>= 4.0.1, < 5.0) |
@@ -554,7 +558,8 @@ DEPENDENCIES |
||
554 | 558 |
delayed_job_active_record! |
555 | 559 |
delorean |
556 | 560 |
devise (~> 3.4.0) |
557 |
- dotenv-rails (~> 2.0.1) |
|
561 |
+ dotenv! |
|
562 |
+ dotenv-rails! |
|
558 | 563 |
dropbox-api |
559 | 564 |
em-http-request (~> 1.1.2) |
560 | 565 |
evernote_oauth |
@@ -595,7 +600,7 @@ DEPENDENCIES |
||
595 | 600 |
omniauth-tumblr |
596 | 601 |
omniauth-twitter |
597 | 602 |
omniauth-wunderlist! |
598 |
- pg |
|
603 |
+ pg (~> 0.18.3) |
|
599 | 604 |
protected_attributes (~> 1.0.8) |
600 | 605 |
pry-rails |
601 | 606 |
quiet_assets |
@@ -165,13 +165,6 @@ You are done installing the database and can go back to the rest of the installa |
||
165 | 165 |
# Copy the example Unicorn config |
166 | 166 |
sudo -u huginn -H cp config/unicorn.rb.example config/unicorn.rb |
167 | 167 |
|
168 |
-### Install Gems |
|
169 |
- |
|
170 |
-**Note:** As of bundler 1.5.2, you can invoke `bundle install -jN` (where `N` the number of your processor cores) and enjoy parallel gem installation with measurable difference in completion time (~60% faster). Check the number of your cores with `nproc`. For more information check this [post](http://robots.thoughtbot.com/parallel-gem-installing-using-bundler). First make sure you have bundler >= 1.5.2 (run `bundle -v`) as it addresses some [issues](https://devcenter.heroku.com/changelog-items/411) that were [fixed](https://github.com/bundler/bundler/pull/2817) in 1.5.2. |
|
171 |
- |
|
172 |
- sudo -u huginn -H bundle install --deployment --without development test |
|
173 |
- |
|
174 |
- |
|
175 | 168 |
### Configure it |
176 | 169 |
|
177 | 170 |
# Update Huginn config file and follow the instructions |
@@ -211,6 +204,11 @@ Change the Unicorn config if needed, the [requirements.md](./requirements.md#uni |
||
211 | 204 |
|
212 | 205 |
**Note:** If you want to use HTTPS, which is what we recommend, see [Using HTTPS](#using-https) for the additional steps. |
213 | 206 |
|
207 |
+### Install Gems |
|
208 |
+ |
|
209 |
+**Note:** As of bundler 1.5.2, you can invoke `bundle install -jN` (where `N` the number of your processor cores) and enjoy parallel gem installation with measurable difference in completion time (~60% faster). Check the number of your cores with `nproc`. For more information check this [post](http://robots.thoughtbot.com/parallel-gem-installing-using-bundler). First make sure you have bundler >= 1.5.2 (run `bundle -v`) as it addresses some [issues](https://devcenter.heroku.com/changelog-items/411) that were [fixed](https://github.com/bundler/bundler/pull/2817) in 1.5.2. |
|
210 |
+ |
|
211 |
+ sudo -u huginn -H bundle install --deployment --without development test |
|
214 | 212 |
|
215 | 213 |
### Initialize Database |
216 | 214 |
|
@@ -0,0 +1,38 @@ |
||
1 |
+class GemfileHelper |
|
2 |
+ class << self |
|
3 |
+ def load_dotenv |
|
4 |
+ dotenv_dir = Dir[File.join(File.dirname(__FILE__), '../vendor/gems/dotenv-[0-9]*')].sort.last |
|
5 |
+ |
|
6 |
+ yield dotenv_dir |
|
7 |
+ |
|
8 |
+ $:.unshift File.join(dotenv_dir, 'lib') |
|
9 |
+ require "dotenv" |
|
10 |
+ $:.shift |
|
11 |
+ |
|
12 |
+ root = Pathname.new(File.join(File.dirname(__FILE__), '..')) |
|
13 |
+ sanity_check Dotenv.load( |
|
14 |
+ root.join(".env.local"), |
|
15 |
+ root.join(".env.#{ENV['RAILS_ENV']}"), |
|
16 |
+ root.join(".env") |
|
17 |
+ ) |
|
18 |
+ end |
|
19 |
+ |
|
20 |
+ private |
|
21 |
+ |
|
22 |
+ def sanity_check(env) |
|
23 |
+ return if ENV['CI'] == 'true' || !env.empty? |
|
24 |
+ puts warning |
|
25 |
+ raise "Could not load huginn settings from .env file." |
|
26 |
+ end |
|
27 |
+ |
|
28 |
+ def warning |
|
29 |
+ <<-EOF |
|
30 |
+Could not load huginn settings from .env file. |
|
31 |
+ |
|
32 |
+Make sure to copy the .env.example to .env and change it to match your configuration. |
|
33 |
+ |
|
34 |
+Capistrano 2 users: Make sure shared files are symlinked before bundle runs: before 'bundle:install', 'deploy:symlink_configs' |
|
35 |
+EOF |
|
36 |
+ end |
|
37 |
+ end |
|
38 |
+end |
@@ -0,0 +1,22 @@ |
||
1 |
+Copyright (c) 2012 Brandon Keepers |
|
2 |
+ |
|
3 |
+MIT License |
|
4 |
+ |
|
5 |
+Permission is hereby granted, free of charge, to any person obtaining |
|
6 |
+a copy of this software and associated documentation files (the |
|
7 |
+"Software"), to deal in the Software without restriction, including |
|
8 |
+without limitation the rights to use, copy, modify, merge, publish, |
|
9 |
+distribute, sublicense, and/or sell copies of the Software, and to |
|
10 |
+permit persons to whom the Software is furnished to do so, subject to |
|
11 |
+the following conditions: |
|
12 |
+ |
|
13 |
+The above copyright notice and this permission notice shall be |
|
14 |
+included in all copies or substantial portions of the Software. |
|
15 |
+ |
|
16 |
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
17 |
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
18 |
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
19 |
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
20 |
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
21 |
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
22 |
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
@@ -0,0 +1,136 @@ |
||
1 |
+# dotenv [](https://travis-ci.org/bkeepers/dotenv) |
|
2 |
+ |
|
3 |
+Shim to load environment variables from `.env` into `ENV` in *development*. |
|
4 |
+ |
|
5 |
+Storing [configuration in the environment](http://www.12factor.net/config) is one of the tenets of a [twelve-factor app](http://www.12factor.net/). Anything that is likely to change between deployment environments–such as resource handles for databases or credentials for external services–should be extracted from the code into environment variables. |
|
6 |
+ |
|
7 |
+But it is not always practical to set environment variables on development machines or continuous integration servers where multiple projects are run. dotenv loads variables from a `.env` file into `ENV` when the environment is bootstrapped. |
|
8 |
+ |
|
9 |
+## Installation |
|
10 |
+ |
|
11 |
+### Rails |
|
12 |
+ |
|
13 |
+Add this line to the top of your application's Gemfile: |
|
14 |
+ |
|
15 |
+```ruby |
|
16 |
+gem 'dotenv-rails', :groups => [:development, :test] |
|
17 |
+``` |
|
18 |
+ |
|
19 |
+And then execute: |
|
20 |
+ |
|
21 |
+```shell |
|
22 |
+$ bundle |
|
23 |
+``` |
|
24 |
+ |
|
25 |
+#### Note on load order |
|
26 |
+ |
|
27 |
+dotenv is initialized in your Rails app during the `before_configuration` callback, which is fired when the `Application` constant is defined in `config/application.rb` with `class Application < Rails::Application`. If you need it to be initialized sooner, you can manually call `Dotenv::Railtie.load`. |
|
28 |
+ |
|
29 |
+```ruby |
|
30 |
+# config/application.rb |
|
31 |
+Bundler.require(*Rails.groups) |
|
32 |
+ |
|
33 |
+Dotenv::Railtie.load |
|
34 |
+ |
|
35 |
+HOSTNAME = ENV['HOSTNAME'] |
|
36 |
+``` |
|
37 |
+ |
|
38 |
+If you use gems that require environment variables to be set before they are loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and require `dotenv/rails-now`. |
|
39 |
+ |
|
40 |
+```ruby |
|
41 |
+gem 'dotenv-rails', :require => 'dotenv/rails-now' |
|
42 |
+gem 'gem-that-requires-env-variables' |
|
43 |
+``` |
|
44 |
+ |
|
45 |
+### Sinatra or Plain ol' Ruby |
|
46 |
+ |
|
47 |
+Install the gem: |
|
48 |
+ |
|
49 |
+```shell |
|
50 |
+$ gem install dotenv |
|
51 |
+``` |
|
52 |
+ |
|
53 |
+As early as possible in your application bootstrap process, load `.env`: |
|
54 |
+ |
|
55 |
+```ruby |
|
56 |
+require 'dotenv' |
|
57 |
+Dotenv.load |
|
58 |
+``` |
|
59 |
+ |
|
60 |
+Alternatively, you can use the `dotenv` executable to launch your application: |
|
61 |
+ |
|
62 |
+```shell |
|
63 |
+$ dotenv ./script.py |
|
64 |
+``` |
|
65 |
+ |
|
66 |
+To ensure `.env` is loaded in rake, load the tasks: |
|
67 |
+ |
|
68 |
+```ruby |
|
69 |
+require 'dotenv/tasks' |
|
70 |
+ |
|
71 |
+task :mytask => :dotenv do |
|
72 |
+ # things that require .env |
|
73 |
+end |
|
74 |
+``` |
|
75 |
+ |
|
76 |
+## Usage |
|
77 |
+ |
|
78 |
+Add your application configuration to your `.env` file in the root of your project: |
|
79 |
+ |
|
80 |
+```shell |
|
81 |
+S3_BUCKET=YOURS3BUCKET |
|
82 |
+SECRET_KEY=YOURSECRETKEYGOESHERE |
|
83 |
+``` |
|
84 |
+ |
|
85 |
+If you need multiline variables, for example private keys, you can double quote strings and use the `\n` character for newlines: |
|
86 |
+ |
|
87 |
+```shell |
|
88 |
+PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----\nHkVN9…\n-----END DSA PRIVATE KEY-----\n" |
|
89 |
+``` |
|
90 |
+ |
|
91 |
+You may also add `export` in front of each line so you can `source` the file in bash: |
|
92 |
+ |
|
93 |
+```shell |
|
94 |
+export S3_BUCKET=YOURS3BUCKET |
|
95 |
+export SECRET_KEY=YOURSECRETKEYGOESHERE |
|
96 |
+``` |
|
97 |
+ |
|
98 |
+Whenever your application loads, these variables will be available in `ENV`: |
|
99 |
+ |
|
100 |
+```ruby |
|
101 |
+config.fog_directory = ENV['S3_BUCKET'] |
|
102 |
+``` |
|
103 |
+ |
|
104 |
+Comments may be added to your file as such: |
|
105 |
+ |
|
106 |
+```shell |
|
107 |
+# This is a comment |
|
108 |
+SECRET_KEY=YOURSECRETKEYGOESHERE # comment |
|
109 |
+SECRET_HASH="something-with-a-#-hash" |
|
110 |
+``` |
|
111 |
+ |
|
112 |
+Variable names may not contain the `#` symbol. Values can use the `#` if they are enclosed in quotes. |
|
113 |
+ |
|
114 |
+## Multiple Rails Environments |
|
115 |
+ |
|
116 |
+dotenv was originally created to load configuration variables into `ENV` in *development*. There are typically better ways to manage configuration in production environments - such as `/etc/environment` managed by [Puppet](https://github.com/puppetlabs/puppet) or [Chef](https://github.com/opscode/chef), `heroku config`, etc. |
|
117 |
+ |
|
118 |
+However, some find dotenv to be a convenient way to configure Rails applications in staging and production environments, and you can do that by defining environment-specific files like `.env.production` or `.env.test`. |
|
119 |
+ |
|
120 |
+You can also `.env.local` for local overrides. |
|
121 |
+ |
|
122 |
+## Should I commit my .env file? |
|
123 |
+ |
|
124 |
+Credentials should only be accessible on the machines that need access to them. Never commit sensitive information to a repository that is not needed by every development machine and server. |
|
125 |
+ |
|
126 |
+Personally, I prefer to commit the `.env` file with development-only settings. This makes it easy for other developers to get started on the project without compromising credentials for other environments. If you follow this advice, make sure that all the credentials for your development environment are different from your other deployments and that the development credentials do not have access to any confidential data. |
|
127 |
+ |
|
128 |
+## Contributing |
|
129 |
+ |
|
130 |
+If you want a better idea of how dotenv works, check out the [Ruby Rogues Code Reading of dotenv](https://www.youtube.com/watch?v=lKmY_0uY86s). |
|
131 |
+ |
|
132 |
+1. Fork it |
|
133 |
+2. Create your feature branch (`git checkout -b my-new-feature`) |
|
134 |
+3. Commit your changes (`git commit -am 'Added some feature'`) |
|
135 |
+4. Push to the branch (`git push origin my-new-feature`) |
|
136 |
+5. Create new Pull Request |
@@ -0,0 +1,4 @@ |
||
1 |
+#!/usr/bin/env ruby |
|
2 |
+ |
|
3 |
+require "dotenv/cli" |
|
4 |
+Dotenv::CLI.new(ARGV).run |
@@ -0,0 +1,17 @@ |
||
1 |
+require File.expand_path("../lib/dotenv/version", __FILE__) |
|
2 |
+require "English" |
|
3 |
+ |
|
4 |
+Gem::Specification.new "dotenv-rails", Dotenv::VERSION do |gem| |
|
5 |
+ gem.authors = ["Brandon Keepers"] |
|
6 |
+ gem.email = ["brandon@opensoul.org"] |
|
7 |
+ gem.description = gem.summary = "Autoload dotenv in Rails." |
|
8 |
+ gem.homepage = "https://github.com/bkeepers/dotenv" |
|
9 |
+ gem.license = "MIT" |
|
10 |
+ gem.files = `git ls-files lib | grep rails` |
|
11 |
+ .split($OUTPUT_RECORD_SEPARATOR) + ["README.md", "LICENSE"] |
|
12 |
+ |
|
13 |
+ gem.add_dependency "dotenv", Dotenv::VERSION |
|
14 |
+ |
|
15 |
+ gem.add_development_dependency "spring" |
|
16 |
+ gem.add_development_dependency "railties", "~>4.0" |
|
17 |
+end |
@@ -0,0 +1,18 @@ |
||
1 |
+require File.expand_path("../lib/dotenv/version", __FILE__) |
|
2 |
+require "English" |
|
3 |
+ |
|
4 |
+Gem::Specification.new "dotenv", Dotenv::VERSION do |gem| |
|
5 |
+ gem.authors = ["Brandon Keepers"] |
|
6 |
+ gem.email = ["brandon@opensoul.org"] |
|
7 |
+ gem.description = gem.summary = "Loads environment variables from `.env`." |
|
8 |
+ gem.homepage = "https://github.com/bkeepers/dotenv" |
|
9 |
+ gem.license = "MIT" |
|
10 |
+ |
|
11 |
+ gem.files = `git ls-files README.md LICENSE lib bin | grep -v rails` |
|
12 |
+ .split($OUTPUT_RECORD_SEPARATOR) |
|
13 |
+ gem.executables = gem.files.grep(/^bin\//).map { |f| File.basename(f) } |
|
14 |
+ |
|
15 |
+ gem.add_development_dependency "rake" |
|
16 |
+ gem.add_development_dependency "rspec" |
|
17 |
+ gem.add_development_dependency "rubocop" |
|
18 |
+end |
@@ -0,0 +1 @@ |
||
1 |
+require "dotenv/rails" |
@@ -0,0 +1,62 @@ |
||
1 |
+require "dotenv/parser" |
|
2 |
+require "dotenv/environment" |
|
3 |
+ |
|
4 |
+# The top level Dotenv module. The entrypoint for the application logic. |
|
5 |
+module Dotenv |
|
6 |
+ class << self |
|
7 |
+ attr_accessor :instrumenter |
|
8 |
+ end |
|
9 |
+ |
|
10 |
+ module_function |
|
11 |
+ |
|
12 |
+ def load(*filenames) |
|
13 |
+ with(*filenames) do |f| |
|
14 |
+ ignoring_nonexistent_files do |
|
15 |
+ env = Environment.new(f) |
|
16 |
+ instrument("dotenv.load", :env => env) { env.apply } |
|
17 |
+ end |
|
18 |
+ end |
|
19 |
+ end |
|
20 |
+ |
|
21 |
+ # same as `load`, but raises Errno::ENOENT if any files don't exist |
|
22 |
+ def load!(*filenames) |
|
23 |
+ with(*filenames) do |f| |
|
24 |
+ env = Environment.new(f) |
|
25 |
+ instrument("dotenv.load", :env => env) { env.apply } |
|
26 |
+ end |
|
27 |
+ end |
|
28 |
+ |
|
29 |
+ # same as `load`, but will override existing values in `ENV` |
|
30 |
+ def overload(*filenames) |
|
31 |
+ with(*filenames) do |f| |
|
32 |
+ ignoring_nonexistent_files do |
|
33 |
+ env = Environment.new(f) |
|
34 |
+ instrument("dotenv.overload", :env => env) { env.apply! } |
|
35 |
+ end |
|
36 |
+ end |
|
37 |
+ end |
|
38 |
+ |
|
39 |
+ # Internal: Helper to expand list of filenames. |
|
40 |
+ # |
|
41 |
+ # Returns a hash of all the loaded environment variables. |
|
42 |
+ def with(*filenames, &block) |
|
43 |
+ filenames << ".env" if filenames.empty? |
|
44 |
+ |
|
45 |
+ filenames.reduce({}) do |hash, filename| |
|
46 |
+ hash.merge! block.call(File.expand_path(filename)) || {} |
|
47 |
+ end |
|
48 |
+ end |
|
49 |
+ |
|
50 |
+ def instrument(name, payload = {}, &block) |
|
51 |
+ if instrumenter |
|
52 |
+ instrumenter.instrument(name, payload, &block) |
|
53 |
+ else |
|
54 |
+ block.call |
|
55 |
+ end |
|
56 |
+ end |
|
57 |
+ |
|
58 |
+ def ignoring_nonexistent_files |
|
59 |
+ yield |
|
60 |
+ rescue Errno::ENOENT |
|
61 |
+ end |
|
62 |
+end |
@@ -0,0 +1,36 @@ |
||
1 |
+require "dotenv" |
|
2 |
+ |
|
3 |
+module Dotenv |
|
4 |
+ # The CLI is a class responsible of handling all the command line interface |
|
5 |
+ # logic. |
|
6 |
+ class CLI |
|
7 |
+ attr_reader :argv |
|
8 |
+ |
|
9 |
+ def initialize(argv = []) |
|
10 |
+ @argv = argv.dup |
|
11 |
+ end |
|
12 |
+ |
|
13 |
+ def run |
|
14 |
+ filenames = parse_filenames || [] |
|
15 |
+ begin |
|
16 |
+ Dotenv.load!(*filenames) |
|
17 |
+ rescue Errno::ENOENT => e |
|
18 |
+ abort e.message |
|
19 |
+ else |
|
20 |
+ exec(*argv) unless argv.empty? |
|
21 |
+ end |
|
22 |
+ end |
|
23 |
+ |
|
24 |
+ private |
|
25 |
+ |
|
26 |
+ def parse_filenames |
|
27 |
+ pos = argv.index("-f") |
|
28 |
+ return nil unless pos |
|
29 |
+ # drop the -f |
|
30 |
+ argv.delete_at pos |
|
31 |
+ # parse one or more comma-separated .env files |
|
32 |
+ require "csv" |
|
33 |
+ CSV.parse_line argv.delete_at(pos) |
|
34 |
+ end |
|
35 |
+ end |
|
36 |
+end |
@@ -0,0 +1,28 @@ |
||
1 |
+module Dotenv |
|
2 |
+ # This class inherits from Hash and represents the environemnt into which |
|
3 |
+ # Dotenv will load key value pairs from a file. |
|
4 |
+ class Environment < Hash |
|
5 |
+ attr_reader :filename |
|
6 |
+ |
|
7 |
+ def initialize(filename) |
|
8 |
+ @filename = filename |
|
9 |
+ load |
|
10 |
+ end |
|
11 |
+ |
|
12 |
+ def load |
|
13 |
+ update Parser.call(read) |
|
14 |
+ end |
|
15 |
+ |
|
16 |
+ def read |
|
17 |
+ File.read(@filename) |
|
18 |
+ end |
|
19 |
+ |
|
20 |
+ def apply |
|
21 |
+ each { |k, v| ENV[k] ||= v } |
|
22 |
+ end |
|
23 |
+ |
|
24 |
+ def apply! |
|
25 |
+ each { |k, v| ENV[k] = v } |
|
26 |
+ end |
|
27 |
+ end |
|
28 |
+end |
@@ -0,0 +1,93 @@ |
||
1 |
+require "dotenv/substitutions/variable" |
|
2 |
+require "dotenv/substitutions/command" if RUBY_VERSION > "1.8.7" |
|
3 |
+ |
|
4 |
+module Dotenv |
|
5 |
+ class FormatError < SyntaxError; end |
|
6 |
+ |
|
7 |
+ # This class enables parsing of a string for key value pairs to be returned |
|
8 |
+ # and stored in the Environment. It allows for variable substitutions and |
|
9 |
+ # exporting of variables. |
|
10 |
+ class Parser |
|
11 |
+ @substitutions = |
|
12 |
+ Substitutions.constants.map { |const| Substitutions.const_get(const) } |
|
13 |
+ |
|
14 |
+ LINE = / |
|
15 |
+ \A |
|
16 |
+ (?:export\s+)? # optional export |
|
17 |
+ ([\w\.]+) # key |
|
18 |
+ (?:\s*=\s*|:\s+?) # separator |
|
19 |
+ ( # optional value begin |
|
20 |
+ '(?:\'|[^'])*' # single quoted value |
|
21 |
+ | # or |
|
22 |
+ "(?:\"|[^"])*" # double quoted value |
|
23 |
+ | # or |
|
24 |
+ [^#\n]+ # unquoted value |
|
25 |
+ )? # value end |
|
26 |
+ (?:\s*\#.*)? # optional comment |
|
27 |
+ \z |
|
28 |
+ /x |
|
29 |
+ |
|
30 |
+ class << self |
|
31 |
+ attr_reader :substitutions |
|
32 |
+ |
|
33 |
+ def call(string) |
|
34 |
+ new(string).call |
|
35 |
+ end |
|
36 |
+ end |
|
37 |
+ |
|
38 |
+ def initialize(string) |
|
39 |
+ @string = string |
|
40 |
+ @hash = {} |
|
41 |
+ end |
|
42 |
+ |
|
43 |
+ def call |
|
44 |
+ @string.split("\n").each do |line| |
|
45 |
+ parse_line(line) |
|
46 |
+ end |
|
47 |
+ @hash |
|
48 |
+ end |
|
49 |
+ |
|
50 |
+ private |
|
51 |
+ |
|
52 |
+ def parse_line(line) |
|
53 |
+ if (match = line.match(LINE)) |
|
54 |
+ key, value = match.captures |
|
55 |
+ @hash[key] = parse_value(value || "") |
|
56 |
+ elsif line.split.first == "export" |
|
57 |
+ if variable_not_set?(line) |
|
58 |
+ fail FormatError, "Line #{line.inspect} has an unset variable" |
|
59 |
+ end |
|
60 |
+ elsif line !~ /\A\s*(?:#.*)?\z/ # not comment or blank line |
|
61 |
+ fail FormatError, "Line #{line.inspect} doesn't match format" |
|
62 |
+ end |
|
63 |
+ end |
|
64 |
+ |
|
65 |
+ def parse_value(value) |
|
66 |
+ # Remove surrounding quotes |
|
67 |
+ value = value.strip.sub(/\A(['"])(.*)\1\z/, '\2') |
|
68 |
+ |
|
69 |
+ if Regexp.last_match(1) == '"' |
|
70 |
+ value = unescape_characters(expand_newlines(value)) |
|
71 |
+ end |
|
72 |
+ |
|
73 |
+ if Regexp.last_match(1) != "'" |
|
74 |
+ self.class.substitutions.each do |proc| |
|
75 |
+ value = proc.call(value, @hash) |
|
76 |
+ end |
|
77 |
+ end |
|
78 |
+ value |
|
79 |
+ end |
|
80 |
+ |
|
81 |
+ def unescape_characters(value) |
|
82 |
+ value.gsub(/\\([^$])/, '\1') |
|
83 |
+ end |
|
84 |
+ |
|
85 |
+ def expand_newlines(value) |
|
86 |
+ value.gsub('\n', "\n") |
|
87 |
+ end |
|
88 |
+ |
|
89 |
+ def variable_not_set?(line) |
|
90 |
+ !line.split[1..-1].all? { |var| @hash.member?(var) } |
|
91 |
+ end |
|
92 |
+ end |
|
93 |
+end |
@@ -0,0 +1,10 @@ |
||
1 |
+# If you use gems that require environment variables to be set before they are |
|
2 |
+# loaded, then list `dotenv-rails` in the `Gemfile` before those other gems and |
|
3 |
+# require `dotenv/rails-now`. |
|
4 |
+# |
|
5 |
+# gem "dotenv-rails", :require => "dotenv/rails-now" |
|
6 |
+# gem "gem-that-requires-env-variables" |
|
7 |
+# |
|
8 |
+ |
|
9 |
+require "dotenv/rails" |
|
10 |
+Dotenv::Railtie.load |
@@ -0,0 +1,47 @@ |
||
1 |
+require "dotenv" |
|
2 |
+ |
|
3 |
+Dotenv.instrumenter = ActiveSupport::Notifications |
|
4 |
+ |
|
5 |
+# Watch all loaded env files with Spring |
|
6 |
+begin |
|
7 |
+ require "spring/watcher" |
|
8 |
+ ActiveSupport::Notifications.subscribe(/^dotenv/) do |*args| |
|
9 |
+ event = ActiveSupport::Notifications::Event.new(*args) |
|
10 |
+ Spring.watch event.payload[:env].filename if Rails.application |
|
11 |
+ end |
|
12 |
+rescue LoadError |
|
13 |
+ # Spring is not available |
|
14 |
+end |
|
15 |
+ |
|
16 |
+module Dotenv |
|
17 |
+ # Dotenv Railtie for using Dotenv to load environment from a file into |
|
18 |
+ # Rails applications |
|
19 |
+ class Railtie < Rails::Railtie |
|
20 |
+ config.before_configuration { load } |
|
21 |
+ |
|
22 |
+ # Public: Load dotenv |
|
23 |
+ # |
|
24 |
+ # This will get called during the `before_configuration` callback, but you |
|
25 |
+ # can manually call `Dotenv::Railtie.load` if you needed it sooner. |
|
26 |
+ def load |
|
27 |
+ Dotenv.load( |
|
28 |
+ root.join(".env.local"), |
|
29 |
+ root.join(".env.#{Rails.env}"), |
|
30 |
+ root.join(".env") |
|
31 |
+ ) |
|
32 |
+ end |
|
33 |
+ |
|
34 |
+ # Internal: `Rails.root` is nil in Rails 4.1 before the application is |
|
35 |
+ # initialized, so this falls back to the `RAILS_ROOT` environment variable, |
|
36 |
+ # or the current working directory. |
|
37 |
+ def root |
|
38 |
+ Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd) |
|
39 |
+ end |
|
40 |
+ |
|
41 |
+ # Rails uses `#method_missing` to delegate all class methods to the |
|
42 |
+ # instance, which means `Kernel#load` gets called here. We don't want that. |
|
43 |
+ def self.load |
|
44 |
+ instance.load |
|
45 |
+ end |
|
46 |
+ end |
|
47 |
+end |
@@ -0,0 +1,41 @@ |
||
1 |
+require "English" |
|
2 |
+ |
|
3 |
+module Dotenv |
|
4 |
+ module Substitutions |
|
5 |
+ # Substitute shell commands in a value. |
|
6 |
+ # |
|
7 |
+ # SHA=$(git rev-parse HEAD) |
|
8 |
+ # |
|
9 |
+ module Command |
|
10 |
+ class << self |
|
11 |
+ INTERPOLATED_SHELL_COMMAND = / |
|
12 |
+ (?<backslash>\\)? # is it escaped with a backslash? |
|
13 |
+ \$ # literal $ |
|
14 |
+ (?<cmd> # collect command content for eval |
|
15 |
+ \( # require opening paren |
|
16 |
+ ([^()]|\g<cmd>)+ # allow any number of non-parens, or balanced |
|
17 |
+ # parens (by nesting the <cmd> expression |
|
18 |
+ # recursively) |
|
19 |
+ \) # require closing paren |
|
20 |
+ ) |
|
21 |
+ /x |
|
22 |
+ |
|
23 |
+ def call(value, _env) |
|
24 |
+ # Process interpolated shell commands |
|
25 |
+ value.gsub(INTERPOLATED_SHELL_COMMAND) do |*| |
|
26 |
+ # Eliminate opening and closing parentheses |
|
27 |
+ command = $LAST_MATCH_INFO[:cmd][1..-2] |
|
28 |
+ |
|
29 |
+ if $LAST_MATCH_INFO[:backslash] |
|
30 |
+ # Command is escaped, don't replace it. |
|
31 |
+ $LAST_MATCH_INFO[0][1..-1] |
|
32 |
+ else |
|
33 |
+ # Execute the command and return the value |
|
34 |
+ `#{command}`.chomp |
|
35 |
+ end |
|
36 |
+ end |
|
37 |
+ end |
|
38 |
+ end |
|
39 |
+ end |
|
40 |
+ end |
|
41 |
+end |
@@ -0,0 +1,34 @@ |
||
1 |
+require "English" |
|
2 |
+ |
|
3 |
+module Dotenv |
|
4 |
+ module Substitutions |
|
5 |
+ # Substitute variables in a value. |
|
6 |
+ # |
|
7 |
+ # HOST=example.com |
|
8 |
+ # URL="https://$HOST" |
|
9 |
+ # |
|
10 |
+ module Variable |
|
11 |
+ class << self |
|
12 |
+ VARIABLE = / |
|
13 |
+ (\\)? # is it escaped with a backslash? |
|
14 |
+ (\$) # literal $ |
|
15 |
+ \{? # allow brace wrapping |
|
16 |
+ ([A-Z0-9_]+) # match the variable |
|
17 |
+ \}? # closing brace |
|
18 |
+ /xi |
|
19 |
+ |
|
20 |
+ def call(value, env) |
|
21 |
+ value.gsub(VARIABLE) do |variable| |
|
22 |
+ match = $LAST_MATCH_INFO |
|
23 |
+ |
|
24 |
+ if match[1] == '\\' |
|
25 |
+ variable[1..-1] |
|
26 |
+ else |
|
27 |
+ env.fetch(match[3]) { ENV[match[3]] } |
|
28 |
+ end |
|
29 |
+ end |
|
30 |
+ end |
|
31 |
+ end |
|
32 |
+ end |
|
33 |
+ end |
|
34 |
+end |
@@ -0,0 +1,7 @@ |
||
1 |
+desc "Load environment settings from .env" |
|
2 |
+task :dotenv do |
|
3 |
+ require "dotenv" |
|
4 |
+ Dotenv.load |
|
5 |
+end |
|
6 |
+ |
|
7 |
+task :environment => :dotenv |
@@ -0,0 +1,3 @@ |
||
1 |
+module Dotenv |
|
2 |
+ VERSION = "2.0.1" |
|
3 |
+end |